package com.sxfq.loansupermarket.backsystem.modular.theTest.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.core.util.DateUtil;
import com.sxfq.loansupermarket.backsystem.core.util.MyDateUtils;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.SearchTheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TestQuestionsInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.ITheTestInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@SuppressWarnings("unused")
@RequestMapping("/theTest")
public class TheTestController extends BaseController {

    @Autowired
    private ITheTestInfoService theTestService;

    private String PREFIX = "/theTest/";
    /**
     * 跳转到订单信息
     */
    @RequestMapping("")
    public String theTestInfoIndex() {
        return PREFIX + "theTestInfo.html";
    }

    /**
     * 跳转到考试列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(SearchTheTestInfo searchTheTestInfo, HttpServletRequest request) {
        Page<TheTestInfo> page = new PageFactory<TheTestInfo>().defaultPage();
        try {
            SearchTheTestInfo searchInfo = createSearchEntity(request);
            List<TheTestInfo> theTestInfoList = theTestService.selectPageListByQueryGetMap(page, searchInfo);
            page.setRecords(theTestInfoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    public SearchTheTestInfo createSearchEntity(HttpServletRequest request) {
        SearchTheTestInfo searchInfo = new SearchTheTestInfo();

        String theTestName = request.getParameter("theTestName");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        if (!CommUtils.isNull(theTestName)) {
            searchInfo.setTheTestName(theTestName);
        }
        if (!CommUtils.isNull(startTime)) {
            searchInfo.setStartTime(startTime);
        }
        if (!CommUtils.isNull(endTime)) {
            Date parse = DateUtil.parse(endTime, "yyyy-MM-dd");
            Date date1 = MyDateUtils.addDays(parse, 1);
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            searchInfo.setEndTime(sd.format(date1));
        }
        return searchInfo;
    }

    /**
     * 跳转到添加考题
     */
//    @Permission
    @RequestMapping(value = "/theTest_add")
    public String to_add(Model model) {
        return PREFIX + "/theTest_add.html";
    }

    /**
     * 删除考试
     */
    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public Tip delete(@PathVariable String id) throws Exception {
        theTestService.delete(id);
        return SUCCESS_TIP;
    }

    /**
     * 修改考试
     */
    @RequestMapping(value = "/theTest_edit/{theTestId}")
    public String to_edit(@PathVariable String theTestId, Model model) {
        if (ToolUtil.isEmpty(theTestId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        TheTestInfo theTestInfo = this.theTestService.selectOneById(theTestId);
        model.addAttribute("theTestInfo", theTestInfo);
        return PREFIX + "theTest_edit.html";
    }

    /**
     * 添加考试
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public int add(@Valid TheTestInfo info, BindingResult result) throws Exception {
        String theTestName = info.getTheTestName().trim();
        int testNameCount = theTestService.selectSameTestNameCount(theTestName);
        if (testNameCount > 0) {
            return 0;
        } else {
            if (result.hasErrors()) {
                throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
            }
            return theTestService.insert(info);
        }
    }

    /**
     * 修改考试
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public int update(@Valid TheTestInfo info, BindingResult result) throws Exception {
        int testNameCount = theTestService.selectSameTestNameCountForUpdate(info.getId(), info.getTheTestName());
        if (testNameCount > 0) {
            return 0;
        } else {
            if (result.hasErrors()) {
                throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
            }
            return theTestService.update(info);
        }
    }
}
